﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.Windows.Media;

namespace SimpleBomberman
{
    public class Game
    {
        #region Members

        private Bomberman _bomberman;
        private Bomb _bomb;
        private List<Level> _levelList;

        private int _levelIndex;

        #endregion Members

        #region Properties

        public Bomberman Bomberman
        {
            get
            {
                return this._bomberman;
            }
            set
            {
                this._bomberman = value;
            }
        }
        public Bomb Bomb
        {
            get
            {
                return this._bomb;
            }
            set
            {
                this._bomb = value;
            }
        }
        public List<Level> LevelList
        {
            get
            {
                return this._levelList;
            }
            set
            {
                this._levelList = value;
            }
        }
        public int LevelIndex
        {
            get
            {
                return this._levelIndex;
            }
            set
            {
                this._levelIndex = value;
            }
        }

        #endregion Properties

        #region Constructors

        public Game()
        {
            this.Bomberman = new Bomberman(1, 1, 30);
            this.Bomb = new Bomb();
            this.LevelList = new List<Level>();

            this.LevelIndex = 0;
        }

        #endregion Constructors

        #region Public Methods

        public void ReadLevels()
        {
            try
            {
                string direction = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                string FullFileName = Path.Combine(direction, "levels.txt");
                //string FullFileName = Path.Combine(Environment.CurrentDirectory, "");//@"\\Files\\levels.txt";

                using (StreamReader sr = new StreamReader(FullFileName))
                {
                    int levelIndex = 0;
                    while (!sr.EndOfStream)
                    {
                        String line = sr.ReadLine();
                        int[,] matrix = new int[11, 19];
                        int k = 0;
                        for (int i = 0; i < matrix.GetLength(0); i++)
                        {
                            for (int j = 0; j < matrix.GetLength(1); j++)
                            {
                                matrix[i, j] = int.Parse(line[k].ToString());
                                k++;
                            }
                        }
                        LevelList.Add(new Level());
                        LevelList[levelIndex].FillLevel(matrix);
                        levelIndex++;
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("The file could not be read:");
                Console.WriteLine(e.Message);
            }
        }
        public void DrawCurrentLevel(Graphics g)
        {
            LevelList[LevelIndex].DrawLevel(g);
        }
        public void Start(ref Game game, ref Graphics graphics, ref Bitmap bitmap, PictureBox pictureBox, InitialForm form)
        {
            game = new Game();
            game.ReadLevels();
            bitmap = new Bitmap(pictureBox.Size.Width,pictureBox.Size.Height);
            pictureBox.Image = bitmap;
            game.LevelList[game.LevelIndex].Time = 0;
            game.LevelIndex = form.CurrentLevelIndex - 1;
            graphics = Graphics.FromImage(bitmap);
            game.DrawCurrentLevel(graphics);
            game.Bomberman.X = 30;
            game.Bomberman.Y = 30;
            game.Bomberman.DeadFlag = false;
            game.Bomberman.Draw(graphics);
            pictureBox.Image = bitmap;
            pictureBox.Refresh();
        }
        public bool CanBombermanMove(KeyEventArgs e)
        {
            int XLocation = 0;
            int YLocation = 0;

            if (e.KeyData == Keys.Down)
            {
                XLocation = this.Bomberman.X;
                YLocation = this.Bomberman.Y + this.Bomberman.Length;
                for (int i = 0; i < this.LevelList[this.LevelIndex].CementWallList.Count; i++)
                {
                    if (XLocation == this.LevelList[this.LevelIndex].CementWallList[i].X &&
                        YLocation == this.LevelList[this.LevelIndex].CementWallList[i].Y)
                    {
                        return false;
                    }
                }
                for (int i = 0; i < this.LevelList[this.LevelIndex].BrickWallList.Count; i++)
                {
                    if (XLocation == this.LevelList[this.LevelIndex].BrickWallList[i].X &&
                        YLocation == this.LevelList[this.LevelIndex].BrickWallList[i].Y)
                    {
                        return false;
                    }
                }
                if (XLocation == this.Bomb.X && YLocation == this.Bomb.Y)
                {
                    return false;
                }
                return true;
            }
            if (e.KeyData == Keys.Up)
            {
                XLocation = this.Bomberman.X;
                YLocation = this.Bomberman.Y - this.Bomberman.Length;
                for (int i = 0; i < this.LevelList[this.LevelIndex].CementWallList.Count; i++)
                {
                    if (XLocation == this.LevelList[this.LevelIndex].CementWallList[i].X &&
                        YLocation == this.LevelList[this.LevelIndex].CementWallList[i].Y)
                    {
                        return false;
                    }
                }
                for (int i = 0; i < this.LevelList[this.LevelIndex].BrickWallList.Count; i++)
                {
                    if (XLocation == this.LevelList[this.LevelIndex].BrickWallList[i].X &&
                        YLocation == this.LevelList[this.LevelIndex].BrickWallList[i].Y)
                    {
                        return false;
                    }
                }
                if (XLocation == this.Bomb.X && YLocation == this.Bomb.Y)
                {
                    return false;
                }
                return true;
            }
            if (e.KeyData == Keys.Left)
            {
                XLocation = this.Bomberman.X - this.Bomberman.Length;
                YLocation = this.Bomberman.Y;
                for (int i = 0; i < this.LevelList[this.LevelIndex].CementWallList.Count; i++)
                {
                    if (XLocation == this.LevelList[this.LevelIndex].CementWallList[i].X &&
                        YLocation == this.LevelList[this.LevelIndex].CementWallList[i].Y)
                    {
                        return false;
                    }
                }
                for (int i = 0; i < this.LevelList[this.LevelIndex].BrickWallList.Count; i++)
                {
                    if (XLocation == this.LevelList[this.LevelIndex].BrickWallList[i].X &&
                        YLocation == this.LevelList[this.LevelIndex].BrickWallList[i].Y)
                    {
                        return false;
                    }
                }
                if (XLocation == this.Bomb.X && YLocation == this.Bomb.Y)
                {
                    return false;
                }
                return true;
            }
            if (e.KeyData == Keys.Right)
            {
                XLocation = this.Bomberman.X + this.Bomberman.Length;
                YLocation = this.Bomberman.Y;
                for (int i = 0; i < this.LevelList[this.LevelIndex].CementWallList.Count; i++)
                {
                    if (XLocation == this.LevelList[this.LevelIndex].CementWallList[i].X &&
                        YLocation == this.LevelList[this.LevelIndex].CementWallList[i].Y)
                    {
                        return false;
                    }
                }
                for (int i = 0; i < this.LevelList[this.LevelIndex].BrickWallList.Count; i++)
                {
                    if (XLocation == this.LevelList[this.LevelIndex].BrickWallList[i].X &&
                        YLocation == this.LevelList[this.LevelIndex].BrickWallList[i].Y)
                    {
                        return false;
                    }
                }
                if (XLocation == this.Bomb.X && YLocation == this.Bomb.Y)
                {
                    return false;
                }
                return true;
            }
            return true;
        }
        public void MoveBomberman()
        {
            if (this.Bomberman.RightFlag == true)
            {
                this.Bomberman.X += this.Bomberman.Length;
            }
            if (this.Bomberman.LeftFlag == true)
            {
                this.Bomberman.X -= this.Bomberman.Length;
            }
            if (this.Bomberman.UpFlag == true)
            {
                this.Bomberman.Y -= this.Bomberman.Length;
            }
            if (this.Bomberman.DownFlag == true)
            {
                this.Bomberman.Y += this.Bomberman.Length;
            }
        }
        public void CheckDefeat(System.Windows.Forms.Timer gameTimer, PictureBox pictureBox, MediaPlayer mediaPlayer)
        {
            for (int i = 0; i < this.LevelList[this.LevelIndex].EnemyList.Count; i++)
            {
                if (this.Bomberman.X == this.LevelList[this.LevelIndex].EnemyList[i].X &&
                    this.Bomberman.Y == this.LevelList[this.LevelIndex].EnemyList[i].Y)
                {
                    this.Bomberman.DeadFlag = true;
                    break;
                }
            }
            if (this.Bomberman.DeadFlag)
            {
                mediaPlayer.Stop();
                string direction = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                string FullFileName = Path.Combine(direction, "LoseSound.mp3");
                mediaPlayer.Open(new Uri(FullFileName));
                mediaPlayer.Volume = 1.0;
                mediaPlayer.Play();
                gameTimer.Enabled = false;
                MessageBox.Show("You lose!");
            }
        }
        public void CheckWin(System.Windows.Forms.Timer gameTimer, MediaPlayer mediaPlayer)
        {
            if (this.Bomberman.IsFinish)
            {
                mediaPlayer.Stop();
                string direction = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                string FullFileName = Path.Combine(direction, "WinSound.mp3");
                mediaPlayer.Open(new Uri(FullFileName));
                mediaPlayer.Volume = 1.0;
                mediaPlayer.Play();
                gameTimer.Enabled = false;
                MessageBox.Show("Congratulation! You won!");
            }
        }
        public void CheckBonusFinding(Label label)
        {
            if (this.Bomberman.X == this.LevelList[this.LevelIndex].Bonus.X &&
                            this.Bomberman.Y == this.LevelList[this.LevelIndex].Bonus.Y)
            {
                Random rd = new Random();
                this.LevelList[this.LevelIndex].Time -= rd.Next(10, 20);
                label.Text = this.LevelList[this.LevelIndex].Points.ToString();
                this.LevelList[this.LevelIndex].Bonus.X = 0;
                this.LevelList[this.LevelIndex].Bonus.Y = 0;
            }
        }
        public void CheckExitDoorFinding()
        {
            if (this.Bomberman.X == this.LevelList[this.LevelIndex].ExitDoor.X && 
                this.Bomberman.Y == this.LevelList[this.LevelIndex].ExitDoor.Y)
            {
                this.Bomberman.IsFinish = true;
            }
        }
        public void Explosion(Graphics g, MediaPlayer mediaPlayer, Label label)
        {
            //--- Is active? ---
            if (this.Bomb.X != 0 && this.Bomb.Y != 0)
            {
                if (this.Bomb.DetonationsCounter > 0)
                {
                    this.Bomb.DetonationsCounter--;
                }
                else
                {
                    string direction = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                    string FullFileName = Path.Combine(direction, "BombBang.mp3");
                    mediaPlayer.Open(new Uri(FullFileName));
                    mediaPlayer.Volume = 0.3;
                    mediaPlayer.Play();

                    //--- BrickWall ---
                    for (int i = 0; i < this.LevelList[this.LevelIndex].BrickWallList.Count; i++)
                    {
                        //--- down ---
                        if ((this.LevelList[this.LevelIndex].BrickWallList[i].X == this.Bomb.X &&
                            this.LevelList[this.LevelIndex].BrickWallList[i].Y == this.Bomb.Y + this.Bomb.Length))
                        {
                            this.LevelList[this.LevelIndex].BrickWallList.RemoveAt(i);
                            g.FillRectangle(System.Drawing.Brushes.Green, this.Bomb.X, this.Bomb.Y + this.Bomb.Length, this.Bomb.Length, this.Bomb.Length);
                            i = 0;
                            this.LevelList[this.LevelIndex].Points += 100;
                            continue;
                        }
                        //--- left ---
                        if ((this.LevelList[this.LevelIndex].BrickWallList[i].X == this.Bomb.X - this.Bomb.Length &&
                            this.LevelList[this.LevelIndex].BrickWallList[i].Y == this.Bomb.Y))
                        {
                            this.LevelList[this.LevelIndex].BrickWallList.RemoveAt(i);
                            g.FillRectangle(System.Drawing.Brushes.Green, this.Bomb.X - this.Bomb.Length, this.Bomb.Y, this.Bomb.Length, this.Bomb.Length);
                            i = 0;
                            this.LevelList[this.LevelIndex].Points += 100;
                            continue;
                        }
                        //--- up ---
                        if ((this.LevelList[this.LevelIndex].BrickWallList[i].X == this.Bomb.X &&
                            this.LevelList[this.LevelIndex].BrickWallList[i].Y == this.Bomb.Y - this.Bomb.Length))
                        {
                            this.LevelList[this.LevelIndex].BrickWallList.RemoveAt(i);
                            g.FillRectangle(System.Drawing.Brushes.Green, this.Bomb.X, this.Bomb.Y - this.Bomb.Length, this.Bomb.Length, this.Bomb.Length);
                            i = 0;
                            this.LevelList[this.LevelIndex].Points += 100;
                            continue;
                        }
                        //--- right ---
                        if ((this.LevelList[this.LevelIndex].BrickWallList[i].X == this.Bomb.X + this.Bomb.Length &&
                            this.LevelList[this.LevelIndex].BrickWallList[i].Y == this.Bomb.Y))
                        {
                            this.LevelList[this.LevelIndex].BrickWallList.RemoveAt(i);
                            g.FillRectangle(System.Drawing.Brushes.Green, this.Bomb.X + this.Bomb.Length, this.Bomb.Y, this.Bomb.Length, this.Bomb.Length);
                            i = 0;
                            this.LevelList[this.LevelIndex].Points += 100;
                            continue;
                        }
                    }
                    //--- Enemy ---
                    for (int i = 0; i < this.LevelList[this.LevelIndex].EnemyList.Count; i++)
                    {
                        //--- down ---
                        if ((this.LevelList[this.LevelIndex].EnemyList[i].X == this.Bomb.X &&
                            this.LevelList[this.LevelIndex].EnemyList[i].Y == this.Bomb.Y + this.Bomb.Length))
                        {
                            this.LevelList[this.LevelIndex].EnemyList.RemoveAt(i);
                            g.FillRectangle(System.Drawing.Brushes.Green, this.Bomb.X, this.Bomb.Y + this.Bomb.Length, this.Bomb.Length, this.Bomb.Length);
                            i = 0;
                            this.LevelList[this.LevelIndex].Points += 200;
                            continue;
                        }
                        //--- left ---
                        if ((this.LevelList[this.LevelIndex].EnemyList[i].X == this.Bomb.X - this.Bomb.Length &&
                            this.LevelList[this.LevelIndex].EnemyList[i].Y == this.Bomb.Y))
                        {
                            this.LevelList[this.LevelIndex].EnemyList.RemoveAt(i);
                            g.FillRectangle(System.Drawing.Brushes.Green, this.Bomb.X - this.Bomb.Length, this.Bomb.Y, this.Bomb.Length, this.Bomb.Length);
                            i = 0;
                            this.LevelList[this.LevelIndex].Points += 200;
                            continue;
                        }
                        //--- up ---
                        if ((this.LevelList[this.LevelIndex].EnemyList[i].X == this.Bomb.X &&
                            this.LevelList[this.LevelIndex].EnemyList[i].Y == this.Bomb.Y - this.Bomb.Length))
                        {
                            this.LevelList[this.LevelIndex].EnemyList.RemoveAt(i);
                            g.FillRectangle(System.Drawing.Brushes.Green, this.Bomb.X, this.Bomb.Y - this.Bomb.Length, this.Bomb.Length, this.Bomb.Length);
                            i = 0;
                            this.LevelList[this.LevelIndex].Points += 200;
                            continue;
                        }
                        //--- right ---
                        if ((this.LevelList[this.LevelIndex].EnemyList[i].X == this.Bomb.X + this.Bomb.Length &&
                            this.LevelList[this.LevelIndex].EnemyList[i].Y == this.Bomb.Y))
                        {
                            this.LevelList[this.LevelIndex].EnemyList.RemoveAt(i);
                            g.FillRectangle(System.Drawing.Brushes.Green, this.Bomb.X + this.Bomb.Length, this.Bomb.Y, this.Bomb.Length, this.Bomb.Length);
                            i = 0;
                            this.LevelList[this.LevelIndex].Points += 200;
                            continue;
                        }
                    }
                    //--- Bomberman ---
                    //--- down ---
                    if (this.Bomberman.X == this.Bomb.X && this.Bomberman.Y == this.Bomb.Y + this.Bomb.Length)
                    {
                        this.Bomberman.DeadFlag = true;
                    }
                    //--- left ---
                    if (this.Bomberman.X == this.Bomb.X - this.Bomb.Length && this.Bomberman.Y == this.Bomb.Y)
                    {
                        this.Bomberman.DeadFlag = true;
                    }
                    //--- up ---
                    if (this.Bomberman.X == this.Bomb.X && this.Bomberman.Y == this.Bomb.Y - this.Bomb.Length)
                    {
                        this.Bomberman.DeadFlag = true;
                    }
                    //--- right ---
                    if (this.Bomberman.X == this.Bomb.X + this.Bomb.Length && this.Bomberman.Y == this.Bomb.Y)
                    {
                        this.Bomberman.DeadFlag = true;
                    }

                    label.Text = this.LevelList[this.LevelIndex].Points.ToString();
                    g.FillRectangle(System.Drawing.Brushes.Green, this.Bomb.X, this.Bomb.Y, this.Bomb.Length, this.Bomb.Length);
                    this.Bomb.X = 0;
                    this.Bomb.Y = 0;
                    this.Bomb.DetonationsCounter = 3;
                }
            }
        }
        public void ReDrawBomb(Graphics graphics)
        {
            if (this.Bomb.X != 0 && this.Bomb.Y != 0)
            {
                this.Bomb.Draw(graphics);
            }
        }
        public void ChooseDirectionAndMoveEnemy(Enemy enemy)
        {
            Random rd = new Random();
            int XLocation = 0;
            int YLocation = 0;
            int CountOfDirections = 4;
            int[] DirectionArray = new int[4] { 1, 1, 1, 1 };

            for (int i = 1; i < 5; i++)
            {
                XLocation = 0;
                YLocation = 0;

                //--- Down ---
                if (i == 1)
                {
                    XLocation = enemy.X;
                    YLocation = enemy.Y + enemy.Length;
                    for (int j = 0; j < this.LevelList[this.LevelIndex].CementWallList.Count; j++)
                    {
                        if (XLocation == this.LevelList[this.LevelIndex].CementWallList[j].X &&
                            YLocation == this.LevelList[this.LevelIndex].CementWallList[j].Y)
                        {
                            CountOfDirections--;
                            DirectionArray[0] = 0;
                            break;
                        }
                    }
                    for (int j = 0; j < this.LevelList[this.LevelIndex].BrickWallList.Count; j++)
                    {
                        if (XLocation == this.LevelList[this.LevelIndex].BrickWallList[j].X &&
                            YLocation == this.LevelList[this.LevelIndex].BrickWallList[j].Y)
                        {
                            CountOfDirections--;
                            DirectionArray[0] = 0;
                            break;
                        }
                    }
                    if (XLocation == this.LevelList[this.LevelIndex].ExitDoor.X &&
                        YLocation == this.LevelList[this.LevelIndex].ExitDoor.Y)
                    {
                        CountOfDirections--;
                        DirectionArray[0] = 0;
                    }
                    if (XLocation == this.LevelList[this.LevelIndex].Bonus.X &&
                        YLocation == this.LevelList[this.LevelIndex].Bonus.Y)
                    {
                        CountOfDirections--;
                        DirectionArray[0] = 0;
                    }
                    if (XLocation == this.Bomb.X && YLocation == this.Bomb.Y)
                    {
                        CountOfDirections--;
                        DirectionArray[0] = 0;
                    }
                    //return true;
                }

                //--- Up ---
                if (i == 2)
                {
                    XLocation = enemy.X;
                    YLocation = enemy.Y - enemy.Length;
                    for (int j = 0; j < this.LevelList[this.LevelIndex].CementWallList.Count; j++)
                    {
                        if (XLocation == this.LevelList[this.LevelIndex].CementWallList[j].X &&
                            YLocation == this.LevelList[this.LevelIndex].CementWallList[j].Y)
                        {
                            CountOfDirections--;
                            DirectionArray[1] = 0;
                            break;
                        }
                    }
                    for (int j = 0; j < this.LevelList[this.LevelIndex].BrickWallList.Count; j++)
                    {
                        if (XLocation == this.LevelList[this.LevelIndex].BrickWallList[j].X &&
                            YLocation == this.LevelList[this.LevelIndex].BrickWallList[j].Y)
                        {
                            CountOfDirections--;
                            DirectionArray[1] = 0;
                            break;
                        }
                    }
                    if (XLocation == this.LevelList[this.LevelIndex].ExitDoor.X &&
                        YLocation == this.LevelList[this.LevelIndex].ExitDoor.Y)
                    {
                        CountOfDirections--;
                        DirectionArray[1] = 0;
                    }
                    if (XLocation == this.LevelList[this.LevelIndex].Bonus.X &&
                        YLocation == this.LevelList[this.LevelIndex].Bonus.Y)
                    {
                        CountOfDirections--;
                        DirectionArray[1] = 0;
                    }
                    if (XLocation == this.Bomb.X && YLocation == this.Bomb.Y)
                    {
                        CountOfDirections--;
                        DirectionArray[1] = 0;
                    }
                    //return true;
                }

                //--- Left ---
                if (i == 3)
                {
                    XLocation = enemy.X - enemy.Length;
                    YLocation = enemy.Y;
                    for (int j = 0; j < this.LevelList[this.LevelIndex].CementWallList.Count; j++)
                    {
                        if (XLocation == this.LevelList[this.LevelIndex].CementWallList[j].X &&
                            YLocation == this.LevelList[this.LevelIndex].CementWallList[j].Y)
                        {
                            CountOfDirections--;
                            DirectionArray[2] = 0;
                            break;
                        }
                    }
                    for (int j = 0; j < this.LevelList[this.LevelIndex].BrickWallList.Count; j++)
                    {
                        if (XLocation == this.LevelList[this.LevelIndex].BrickWallList[j].X &&
                            YLocation == this.LevelList[this.LevelIndex].BrickWallList[j].Y)
                        {
                            CountOfDirections--;
                            DirectionArray[2] = 0;
                            break;
                        }
                    }
                    if (XLocation == this.LevelList[this.LevelIndex].ExitDoor.X &&
                        YLocation == this.LevelList[this.LevelIndex].ExitDoor.Y)
                    {
                        CountOfDirections--;
                        DirectionArray[2] = 0;
                    }
                    if (XLocation == this.LevelList[this.LevelIndex].Bonus.X &&
                        YLocation == this.LevelList[this.LevelIndex].Bonus.Y)
                    {
                        CountOfDirections--;
                        DirectionArray[2] = 0;
                    }
                    if (XLocation == this.Bomb.X && YLocation == this.Bomb.Y)
                    {
                        CountOfDirections--;
                        DirectionArray[2] = 0;
                    }
                    //return true;
                }

                //--- Right ---
                if (i == 4)
                {
                    XLocation = enemy.X + enemy.Length;
                    YLocation = enemy.Y;
                    for (int j = 0; j < this.LevelList[this.LevelIndex].CementWallList.Count; j++)
                    {
                        if (XLocation == this.LevelList[this.LevelIndex].CementWallList[j].X &&
                            YLocation == this.LevelList[this.LevelIndex].CementWallList[j].Y)
                        {
                            CountOfDirections--;
                            DirectionArray[3] = 0;
                            break;
                        }
                    }
                    for (int j = 0; j < this.LevelList[this.LevelIndex].BrickWallList.Count; j++)
                    {
                        if (XLocation == this.LevelList[this.LevelIndex].BrickWallList[j].X &&
                            YLocation == this.LevelList[this.LevelIndex].BrickWallList[j].Y)
                        {
                            CountOfDirections--;
                            DirectionArray[3] = 0;
                            break;
                        }
                    }
                    if (XLocation == this.LevelList[this.LevelIndex].ExitDoor.X &&
                        YLocation == this.LevelList[this.LevelIndex].ExitDoor.Y)
                    {
                        CountOfDirections--;
                        DirectionArray[3] = 0;
                    }
                    if (XLocation == this.LevelList[this.LevelIndex].Bonus.X &&
                        YLocation == this.LevelList[this.LevelIndex].Bonus.Y)
                    {
                        CountOfDirections--;
                        DirectionArray[3] = 0;
                    }
                    if (XLocation == this.Bomb.X && YLocation == this.Bomb.Y)
                    {
                        CountOfDirections--;
                        DirectionArray[3] = 0;
                    }
                    //return true;
                }
            }

            if (CountOfDirections > 0)
            {
                List<int> PossibleWayList = new List<int>();

                for (int i = 0; i < DirectionArray.Length; i++)
                {
                    if (DirectionArray[i] == 1)
                    {
                        PossibleWayList.Add(i + 1);
                    }
                }

                int random = rd.Next(0, 333) % PossibleWayList.Count;
                enemy.Direction = PossibleWayList[random];

                if (enemy.Direction == 1)
                {
                    enemy.DownFlag = true;
                    MoveEnemy(enemy);
                    enemy.DownFlag = false;
                }
                else if (enemy.Direction == 2)
                {
                    enemy.UpFlag = true;
                    MoveEnemy(enemy);
                    enemy.UpFlag = false;
                }
                else if (enemy.Direction == 3)
                {
                    enemy.LeftFlag = true;
                    MoveEnemy(enemy);
                    enemy.LeftFlag = false;
                }
                else if (enemy.Direction == 4)
                {
                    enemy.RightFlag = true;
                    MoveEnemy(enemy);
                    enemy.RightFlag = false;
                }
                PossibleWayList.Clear();

            }
        }
        public void MoveEnemy(Enemy enemy)
        {
            if (enemy.RightFlag == true)
            {
                enemy.X += enemy.Length;
            }
            if (enemy.LeftFlag == true)
            {
                enemy.X -= enemy.Length;
            }
            if (enemy.UpFlag == true)
            {
                enemy.Y -= enemy.Length;
            }
            if (enemy.DownFlag == true)
            {
                enemy.Y += enemy.Length;
            }
        }

        #endregion Public Methods

    }
}
